package com.doctor.comm;

import java.io.IOException;
import java.util.Calendar;

import com.doctor.ui.R;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver{

	/**
	 * 接收闹铃广播
	 */
	private WindowManager manager;
	private LinearLayout view;
	private WindowManager.LayoutParams params;
	private boolean isShowwing;
	private MediaPlayer player;
	
	@Override
	public void onReceive(Context arg0, Intent arg1) {
		// TODO Auto-generated method stub
		if ("AlarmReceiver".equals(arg1.getAction())) {
			int id=arg1.getIntExtra("id", -1);
			String note=arg1.getStringExtra("msg");
			if (id>=ConstConfig.BASE_ID*9) {
				//发送健康提示短信
				String[] array=note.split(ConstConfig.IMAGE_SPLIT);	
				SmsManager smsManager=SmsManager.getDefault();
				Intent sendIntent=new Intent("SENT_SMS_ACTION");
				PendingIntent sendPi=PendingIntent.getBroadcast(arg0.getApplicationContext(), -1, sendIntent, 0);
				smsManager.sendTextMessage(array[1], null, array[2], sendPi, null);
				ContentValues values=new ContentValues();
				values.put("date", System.currentTimeMillis());
				values.put("read", 0);
				values.put("type", 2);
				values.put("address", array[1]);
				values.put("body", array[2]);
				//写入短信库
				arg0.getApplicationContext().getContentResolver().insert(Uri.parse("content://sms/sent"), values);
				Toast.makeText(arg0.getApplicationContext(), array[0], Toast.LENGTH_LONG).show();
				return;
			}
			
			if (!isShowwing) {
				player=null;
				player=MediaPlayer.create(arg0, R.raw.alarm_ring);
				try {
					player.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				player.start();
				player.setLooping(false);
				
				if (note!=null && !"".equals(note)) {
					if (manager==null) {
						manager=(WindowManager) arg0.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
					}
					view=null;
					view=(LinearLayout) View.inflate(arg0.getApplicationContext(), R.layout.confirm_dialog, null);
					params = new WindowManager.LayoutParams();  
					params.type = WindowManager.LayoutParams.TYPE_PHONE;
					params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
					params.width = WindowManager.LayoutParams.WRAP_CONTENT;  
					params.height = WindowManager.LayoutParams.WRAP_CONTENT;
					params.gravity= Gravity.CENTER;
					
					TextView notice=(TextView) view.findViewById(R.id.noticeTxt);
					notice.setText("医师宝提醒您："+note);
					Button button=(Button) view.findViewById(R.id.confirmBtn);
					button.setText("知道了");
					button.setOnClickListener(click);
					
					manager.addView(view, params);
					isShowwing=true;
				}
			}
		}
			
			
	}
	
	/**
	 * 根据日期和时间设置一个定时闹钟
	 * @param activity
	 * @param date
	 * @param time
	 * @param note 提示信息
	 */
	public static void setAlarm(Activity activity,String date,String time, int id, String note){
		if (date!=null && !"".equals(date) && time!=null && !"".equals(time)) {
			Intent intent=new Intent();
			intent.setAction("AlarmReceiver");
			intent.putExtra("msg", note);
			intent.putExtra("id", id);
			PendingIntent pi=PendingIntent.getBroadcast(activity, id, intent, 0);
			
			String[] dateArray=date.split("-");
			String[] timeArray=time.split(":");
			
			Calendar calendar=Calendar.getInstance();
			calendar.set(Calendar.YEAR, Integer.parseInt(dateArray[0]));
			calendar.set(Calendar.MONTH, Integer.parseInt(dateArray[1])-1);
			calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateArray[2]));
			calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeArray[0]));
			calendar.set(Calendar.MINUTE, Integer.parseInt(timeArray[1]));
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			
			AlarmManager manager=(AlarmManager) activity.getSystemService(Activity.ALARM_SERVICE);
			manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
		}
	}
	
	/**
	 * 取消闹钟服务
	 * @param activity
	 * @param alarmId
	 */
	public static void cancelAlarm(Activity activity, int alarmId){
		Intent intent=new Intent();
		intent.setAction("AlarmReceiver");
		PendingIntent pi=PendingIntent.getBroadcast(activity, alarmId, intent, 0);
		AlarmManager manager=(AlarmManager) activity.getSystemService(Activity.ALARM_SERVICE);
		manager.cancel(pi);
	}
	
	
	public static void stopAlarmRing(){
		
	}
	
	
	OnClickListener click=new OnClickListener() {		
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			manager.removeView(view);
			if (player==null) {
				return;
			}
			player.stop();
			player.release();
			player=null;
			isShowwing=false;
		}
	};
	
	
	
}
